import {LaptopOutlined} from '@ant-design/icons-vue';
import {defineComponent, onMounted, ref, watch} from 'vue';
import {useRouter} from 'vue-router';

export default defineComponent({
    components: {
        LaptopOutlined,
    },

    setup() {
        const panes = ref(new Array(1).fill(null).map((_, index) => {
            const id = String(index + 1);
            return {
                title:'菜单一',
                content: ``,
                key: '/index/measurement',
            };
        }));
        const activeKey = ref(panes.value[0].key);
        const $router = useRouter();
        const add = (item) => {
            activeKey.value = item.path;
            for (let i in panes.value) {
                if ((panes.value[i].key === item.path) == true)return
            }
            panes.value.push({
                title: item.name,
                key: activeKey.value,
                parh: item.path
            });
            $router.push({path: item.path})
        };

        const remove = targetKey => {
            let lastIndex = 0;
            panes.value.forEach((pane, i) => {
                if (pane.key === targetKey) {
                    lastIndex = i - 1;
                }
            });
            panes.value = panes.value.filter(pane => pane.key !== targetKey);

            if (panes.value.length && activeKey.value === targetKey) {
                if (lastIndex >= 0) {
                    activeKey.value = panes.value[lastIndex].key;
                } else {
                    activeKey.value = panes.value[0].key;
                }
            }
        };
        watch(() => activeKey.value, (cur) => {
            $router.push({path: cur})

        });
        watch(() => panes, (cur) => {
            console.log(cur)
        });
         const outLogin = () =>{
                    $router.push({path: '/'})
                    message.success('退出登录成功')
                }
        onMounted(()=>{
            if(JSON.parse(sessionStorage.getItem('user')) && JSON.parse(sessionStorage.getItem('user')).level == 0 ){ 
                menu.value  =  [
                    {
                        name: '心情日记',
                        path: '/moodIndex'
                    },
                    {
                        name: '阅读文章',
                        path: '/ListArticles'
                    },
                    {
                        name: '个人中心',
                        path: '/measurement'
                    },
                    {
                        name: 'AI助手',
                        path: '/index/moodIndex'
                    }
                ]
            }  else{
                menu.value  =  [
                    {
                        name: '数据展示',
                        path: '/largeSize'
                    },
                    {
                        name: '文章上传',
                        path: '/article'
                    },
                    {
                        name: '阅读文章',
                        path: '/ListArticles'
                    },
                    {
                        name: '个人中心',
                        path: '/measurement'
                    },
                   
                ]
            }
          
        })
        const menu = ref([])
        const onEdit = targetKey => {
            console.log(targetKey)
            remove(targetKey);
        };

        return {
            panes,
            activeKey,
            onEdit,
            outLogin,
            add,
            menu,
            selectedKeys1: ref(['2']),
            selectedKeys2: ref(['1']),
            collapsed: ref(false),
            openKeys: ref(['sub1']),
        };
    },
});
